package com.twt.recruit.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twt.recruit.entity.Project;
import com.twt.recruit.entity.Question;
import com.twt.recruit.entity.dto.RecruitDate;
import com.twt.recruit.entity.vo.GroupParam1;
import com.twt.recruit.mapper.ClubMapper;
import com.twt.recruit.mapper.ProjectGroupMapper;
import com.twt.recruit.mapper.ProjectMapper;
import com.twt.recruit.mapper.QuestionMapper;
import com.twt.recruit.service.proto.ProjectServiceProto;
import com.twt.recruit.utils.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class ProjectService extends ServiceImpl<ProjectMapper, Project> implements ProjectServiceProto {

    @Resource
    ProjectMapper projectMapper;

    @Resource
    ProjectGroupMapper projectGroupMapper;

    @Resource
    ClubMapper clubMapper;

    @Resource
    QuestionMapper questionMapper;

    public JSONObject getSingleProject(Integer projectId){
        Project project = projectMapper.selectById(projectId);

        List<GroupParam1> groupParam1 = projectMapper.selectGroupsByProjectId(projectId);
        JSONObject re = JSONUtil.parseObj(project);
        re.remove("startTime");
        re.putOpt("startTime", TimeUtil.timestampToDate(project.getStartTime().getTime(), TimeUtil.Y_M_D));
        re.putOpt("clubName", clubMapper.selectById(project.getClubId()).getName());
        JSONArray groups = new JSONArray();
        for (GroupParam1 param1 : groupParam1) {
            LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Question::getGroupId, param1.getGroupId());
            queryWrapper.eq(Question::getProjectId, projectId);
            JSONObject group = JSONUtil.parseObj(param1);
            JSONArray questions = new JSONArray();
            questionMapper.selectList(queryWrapper).forEach(q->{
                JSONObject question = JSONUtil.parseObj(questionMapper.selectById(q.getQuestionId()), false);
                questions.add(question);
            });
            group.putOpt("questions", questions);
            groups.add(group);
        }
        re.putOpt("groups", groups);

        re.putOpt("questions", questionMapper.getPrimeQuestions(projectId));
        return re;

    }

    public boolean isClubProject(Integer projectId, Integer clubId){
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getProjectId, projectId);
        queryWrapper.eq(Project::getClubId, clubId);
        return !projectMapper.selectList(queryWrapper).isEmpty();
    }

    public void delProjectByProjectId(Integer projectId) {
        projectMapper.delProjectByProjectId(projectId);
    }

    public void delProjectGroupByProjectId(Integer projectId) {
        projectGroupMapper.delProjectGroupByProjectId(projectId);
    }

    public List<String> getRecruitDate(Integer projectId) {
        List<String> dateList = new ArrayList<>();

        RecruitDate date = projectMapper.getBeginAndEnd(projectId);
//        List<Timestamp> beginAndEnd = projectMapper.getBeginAndEnd(projectId);
        if (date == null || date.getStartTime() == null || date.getEndTime() == null) {
            return null;
        }

        Timestamp begin = date.getStartTime();
        Timestamp end = date.getEndTime();

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        Date beginDate = new Date(begin.getTime());
        Date endDate = new Date(end.getTime());

        Calendar tempBegin = Calendar.getInstance();
        Calendar tempEnd = Calendar.getInstance();
        tempBegin.setTime(beginDate);
        tempEnd.setTime(endDate);

        while (tempBegin.before(tempEnd)) {
            dateList.add(sdf1.format(tempBegin.getTime()));
            tempBegin.add(Calendar.DAY_OF_YEAR, 1);
        }

        return dateList;
    }
}
